package com.cyz.user.userService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.StudyResource;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.StudyResourceMapper;
import com.cyz.user.userService.StudyResourceService;
import com.cyz.user.userService.StudyResourceTypeIdService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.user.vo.StudyResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 34928
* @description 针对表【study_resource】的数据库操作Service实现
* @createDate 2023-02-22 14:32:26
*/
@Service
public class StudyResourceServiceImpl extends ServiceImpl<StudyResourceMapper, StudyResource>
    implements StudyResourceService {

    @Autowired
    private StudyResourceTypeIdService studyResourceTypeIdService;

    @Override
    public R queryStudyResources() {
        //查询数据
        List<StudyResource> list = list();
        //错误判断
        if(list.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(list);
    }

    @Override
    public R queryStudyResourcesAndTypes() {
        //查询数据
        List<StudyResource> list = list();
        List<StudyResourceVo> studyResourceVos = BeanCopyUtil.copyBeanList(list, StudyResourceVo.class);
        studyResourceVos.forEach(item ->{
            item.setTypes(studyResourceTypeIdService.getTypesByResourceId(item.getId()));
        });
        //错误判断
        if(list.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(studyResourceVos);
    }


}




